//index.js
//获取应用实例aaaa
const app = getApp();
var util = require('../../utils/util.js');
var url = require('../../utils/url.js');
var common = require('../../utils/common.js');
var that;

Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [],
    hotelPhone:'',
    initStartDate1: '',
    initEndDate1: '',
    initStartDate2: '',
    initEndDate2: '',
    startDate: '',
    endDate: '',
    night: 1,
    userInfo: {},
    topImg: '',
    introduce: '',
    imglist: [],
    recommendList: [],
    isShowContent: false,
    isShowLogin: false,
  },

  onLoad: function() {
    that = this;
    //初始化日期
    var startDate = util.getTodayDate(new Date());
    var endDate = util.getTomorrowDate(new Date(), 1);
    var yearAfterDate1 = util.getTomorrowDate(new Date(), 365);
    var yearAfterDate2 = util.getTomorrowDate(new Date(endDate), 365);
    that.setData({
      startDate: startDate,
      startDateFlag: startDate,
      initStartDate1: startDate,
      initEndDate1: yearAfterDate1,
      initStartDate2: endDate,
      initEndDate2: yearAfterDate2,
      endDate: endDate,
      endDateFlag: endDate,
    });
    //定位权限
    wx.getSetting({
      success: res => {
        res.authSetting = {
          "scope.userLocation": true
        }
      }
    });
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.hideLoading();
        that.setData({
          isShowContent: true,
          isShowLogin: false,
        });
        that.getHotelInfo();
      },
      fail: function(res) {
        wx.hideLoading();
        that.setData({
          isShowContent: false,
          isShowLogin: true,
        })
      }
    })

  },

  getHotelInfo: function() {
    wx.request({
      url: url.HOTEL_INFO,
      method: 'POST',
      data: {
        hotelId: 1
      },
      success: function(res) {
        console.log(res)
        var entity = res.data.data;
        var arrs = [];
        for (var i = 0; i < entity.banner.split(',').length; i++) {
          arrs.push(entity.banner.split(',')[i])
        };
        that.setData({
          imgUrls: arrs,
          hotelPhone:entity.tell
        })
      }
    })
  },

  onShow: function() {
    that.getHotelInfo();
  },

  callPhone: function(e) {
    wx.makePhoneCall({
      phoneNumber: that.data.hotelPhone
    })
  },

  bindStartDateChange: function(e) {
    that.setData({
      startDate: e.detail.value,
    })
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
    console.log(that.data.night)
    if (that.data.night < 0) {
      that.setData({
        endDate: util.getTomorrowDate(new Date(that.data.startDate), 1),
        initStartDate2: util.getTomorrowDate(new Date(that.data.startDate), 1),
        initEndDate2: util.getTomorrowDate(new Date(that.data.initStartDate2), 365),
      })
    }
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
  },

  bindEndDateChange: function(e) {
    that.setData({
      endDate: e.detail.value
    })
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
  },

  iv_preview: function(e) {
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: that.data.imglist[index],
      urls: that.data.imglist,
    })
  },

  btn_go_map: function() {
    wx.openLocation({
      latitude: 34.5279894011,
      longitude: 110.0753581524,
      name: "兴和度假山庄",
      address: "陕西省渭南市华阴市华山风景区入口旁",
      scale: 28
    })

  },
  // 搜索房间
  bindSearch: function(e) {
    app.globalData.inDate = that.data.startDate;
    app.globalData.outDate = that.data.endDate;
    wx.navigateTo({
      url: '../../pages/roomList/roomList',
    })
  },

  bindGetUserInfo: function(e) {
    if (e.detail.userInfo) {
      var userInfo = e.detail.userInfo;
      wx.getStorage({
        key: 'code',
        success: function(res) {
          common.showLoading();
          wx.request({
            url: url.WX_LOGIN,
            method: 'POST',
            data: {
              code: res.data,
              avatarUrl: userInfo.avatarUrl,
              city: userInfo.city,
              country: userInfo.country,
              gender: userInfo.gender,
              language: userInfo.language,
              nickName: userInfo.nickName,
              province: userInfo.province
            },
            success: res => {
              console.log(res)
              common.closeLoading();
              if (res.data.code == 0) {
                wx.setStorage({
                  key: 'openid',
                  data: res.data.data,
                })
                that.setData({
                  isShowContent: true,
                  isShowLogin: false,
                })
                common.showTip('登录成功');
              } else {
                common.showTip('登录失败，请重试');
              }
            },
            fail: res => {
              common.showTip('登录失败，请重试')
            }
          })
        },
      })
    }
  }

})